package ArtificialInteligence;

import Modelos.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import logicGame.Cards.Card;
import logicGame.Cards.Move;
import logicGame.Player;
import logicGame.States.*;

public class Ia implements Observer {

    ModeloTabuleiro mT;
    ModeloCartas mC;

    HashMap<Integer, ArrayList<Integer>> move;

    ArrayList<Integer> choose;

    public Ia(ModeloTabuleiro m, ModeloCartas mx) {
        this.mT = m;
        this.mT.addObserver(this);
        this.mC = mx;
        this.mC.addObserver(this);

        choose = new ArrayList<>();
        move = new HashMap<>();

    }

    @Override
    public void update(Observable o, Object arg) {

        if (mT.getActivePlayer().isIa()) {

            if (mT.getState() instanceof Auction) {
                mT.bet(new Random().nextInt(mT.getActivePlayer().getCoin() / 2));

            } else if (mT.getState() instanceof BuyCard) {
                if (mT.getActivePlayer().getCoin() >= 3) {
                    mC.buyCard(new Random().nextInt(6));
                } else {
                    mC.buyCard(0);
                }

            } else if (mT.getState() instanceof PickPlace) {
                Card c = mC.getlast();

                if (c.getAction() instanceof Move) {

                    for (int i = 0; i < 23; i++) {
                        for (int j = 0; j < 23; j++) {
                            if (mC.checkMovement(i, j)) {
                                if (move.get(i).isEmpty()) {
                                    choose.add(i);
                                    move.put(i, new ArrayList<Integer>());
                                }
                                move.get(i).add(j);
                            }
                        }
                    }
                    int init = choose.get(new Random().nextInt(choose.size()));
                    int end = move.get(init).get(new Random().nextInt(move.size()));

                    mT.doAction(init);
                    mT.doAction(end);

                    choose.clear();
                    move.clear();

                    //outros movimentos que nao sejam o mover exercitos
                } else {

                    for (int i = 0; i < 23; i++) {
                        if (mC.checkMovement(-1, i)) {
                            choose.add(i);
                        }
                    }
                    mT.doAction(choose.get(new Random().nextInt(choose.size())));
                    choose.clear();
                }
            } else if (mT.getState() instanceof Or) {

                Random randomGenerator = new Random();
                int rand = randomGenerator.nextInt(99);

                if (rand > 49) {
                    mT.chooseAction(1);
                } else {
                    mT.chooseAction(2);
                }

            } else if (mT.getState() instanceof And) {
                Card c = mC.getlast();

                if (c.getOp() == 0) {

                    Random randomGenerator = new Random();
                    int rand = randomGenerator.nextInt(99);

                    if (rand > 49) {
                        mT.chooseAction(1);
                    } else {
                        mT.chooseAction(2);
                    }
                    
                }else if(c.getOp()== 1){
                    mT.chooseAction(2);
                }
                else if(c.getOp()==2){
                    mT.chooseAction(1);
                }
            } else if(mT.getState() instanceof PickArmy){
                
                ArrayList <Player> p = mT.getPTroops();
                
                Random randomGenerator = new Random();
                int rand = randomGenerator.nextInt(p.size());
                
                
            }
        }
    }

}
